var _excluded = ['options', 'searchable', 'className'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Mentions as AntdMentions } from 'antd';
import 'antd/es/mentions/style/index.js';
import classnames from 'classnames';
import React, { forwardRef, useEffect, useMemo } from 'react';
import { useFieldContext } from '../_utils/contexts/FieldContext';
import useRequestOptions from '../_utils/hooks/useRequestOptions';
import createField from '../createField';
/**
 * 提及组件
 */
export var Mentions = /*#__PURE__*/ forwardRef(function (_ref, ref) {
  var _ref2;
  var optionsOrRequest = _ref.options,
    searchable = _ref.searchable,
    className = _ref.className,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useFieldContext = useFieldContext(),
    label = _useFieldContext.label;
  var _useRequestOptions = useRequestOptions(optionsOrRequest),
    options = _useRequestOptions.options,
    queryOptions = _useRequestOptions.queryOptions;
  var defaultPropsForSearch = useMemo(
    function () {
      if (searchable && typeof optionsOrRequest === 'function') {
        return {
          onSearch: queryOptions,
          filterOption: function filterOption() {
            return true;
          },
        };
      }
      return {};
    },
    [searchable, optionsOrRequest, queryOptions],
  );
  useEffect(
    function () {
      var existed =
        rest !== null && rest !== void 0 && rest.value && options.length
          ? options.some(function (option) {
              var _rest$value;
              return (_rest$value = rest.value) === null || _rest$value === void 0
                ? void 0
                : _rest$value.includes(option.value);
            })
          : false;
      if (rest !== null && rest !== void 0 && rest.value && !existed) {
        var _rest$onChange;
        (_rest$onChange = rest.onChange) === null || _rest$onChange === void 0
          ? void 0
          : _rest$onChange.call(rest, []);
      }
    },
    [options],
  );
  return /*#__PURE__*/ React.createElement(
    AntdMentions,
    _extends(
      {
        placeholder: '\u8BF7\u9009\u62E9'.concat(label),
      },
      defaultPropsForSearch,
      {
        className: classnames('szzj-mentions', className),
        options: (_ref2 = options) !== null && _ref2 !== void 0 ? _ref2 : [],
        ref: ref,
      },
      rest,
    ),
  );
});
export default createField(Mentions);
